package com.zrrd.yunchmall.order.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.order.entity.OrderReturnApply;
import com.zrrd.yunchmall.order.service.IOrderReturnApplyService;
import com.zrrd.yunchmall.order.service.impl.OrderReturnApplyServiceImpl;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import io.swagger.models.properties.UUIDProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;

/**
 * <p>
 * 订单退货申请 前端控制器
 * </p>
 *
 * @author YeKong
 * @since 2024-01-15
 */
@SuppressWarnings("all")
@Api(tags = "订单服务-退货处理")
@RestController
@RequestMapping("/returnApply")
public class OrderReturnApplyController {

    @Autowired
    private IOrderReturnApplyService applyService;

    @ApiOperation("退货处理")
    @PostMapping("/update/status/{id}")
    public ResponseResult updateStatus(@PathVariable Integer id, @RequestBody OrderReturnApply apply) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        updateWrapper.eq("id", id);
        updateWrapper.set("status", apply.getStatus());
        updateWrapper.set("handle_man", apply.getHandleMan());
        updateWrapper.set("handle_note", apply.getHandleNote());
        updateWrapper.set("handle_time", apply.getHandleTime());
        if (apply.getStatus() == 1) {
            updateWrapper.set("company_address_id", apply.getCompanyAddressId());
            updateWrapper.set("return_amount", apply.getReturnAmount());
        }
        if (apply.getStatus() == 2) {
            updateWrapper.set("receive_man", "当前登录的用户");
            updateWrapper.set("receive_time", LocalDateTime.now());
            updateWrapper.set("receive_note", apply.getReceiveNote());
        }
        applyService.update(updateWrapper);
        return new ResponseResult(200, "处理完毕");
    }

    @ApiOperation("获取")
    @GetMapping("/{id}")
    public ResponseResult detail(@PathVariable long id) {
        OrderReturnApply orderReturnApply = applyService.getById(id);
        return new ResponseResult(200, "获取成功", orderReturnApply);
    }

    @GetMapping("/list")
    @ApiOperation("处理记录列表")
    public ResponseResult list(  @RequestParam int pageNum,
                                 @RequestParam int pageSize,
                                 Integer id ,Integer status, String handleMan,
                                 String createTime,String handleTIme) {
        QueryWrapper queryWrapper = new QueryWrapper();
        if (id!=null ){
            queryWrapper.eq("id",id);

        }else {
            if (status!=null){
                queryWrapper.eq("status",status);
            }
            if (!StringUtils.isEmpty(handleMan)){
                queryWrapper.like("handle_man",handleMan);
            }
            if (!StringUtils.isEmpty(createTime)){
                queryWrapper.between("create_time",createTime+" 00:00:00",createTime+" 23:59:59");
            }
            if (!StringUtils.isEmpty(handleTIme)){
                queryWrapper.between("handle_time",handleMan+" 00:00:00",handleTIme+" 23:59:59");
            }
        }
        return new ResponseResult(200,"查询成功"
                ,applyService.page(new Page<>(pageNum,pageSize),queryWrapper));
    }
}
